<?php

class Admini_ManagesystemController extends Uuch_ManageController{
    
    public function init() {
        parent::init();
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');     
    }
    
    public function indexAction(){
        
        
    }
    
    
    public function countryAction(){
        
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
        $db = $bootstrap->getResource('db');
        $this->view->city_list = $db->fetchAll(
                        "SELECT id,`name` FROM uuch_city"
                );
    }
    
    public function cityqueryAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('name') != '') {
			$where .= $db->quoteInto(" AND m.`name` LIKE ?", '%' . $this->_getParam('name') . '%');
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$count = $db->fetchOne("SELECT COUNT(*) FROM uuch_city m WHERE $where");
		$rows = $db->fetchAll("SELECT m.*,IFNULL(s.`name`, '&nbsp;') parentName FROM uuch_city m LEFT JOIN uuch_city s ON m.parentId = s.id WHERE $where ORDER BY m.sortid LIMIT $pageSize");
		echo '{"count":"' . $count . '","items":' . json_encode($rows) . '}';
		exit;
	}
        
        public function citypagerAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = '1=1';
		if ($this->_getParam('name') != '') {
			$where .= $db->quoteInto(' AND m.`name` LIKE ?', '%' . $this->_getParam('name') . '%');
		}
		$pageSize = $this->_getParam('pagesize') > 0 ? $this->_getParam('pagesize') : 15;
		$pageIndex = $this->_getParam('pageindex') > 0 ? $this->_getParam('pageindex') : 1;
		$offset = ($pageIndex - 1) * $pageSize;
		$rows = $db->fetchAll("SELECT m.*,s.`name` parentName FROM uuch_city m LEFT JOIN uuch_city s ON m.parentId = s.id WHERE $where ORDER BY m.sortid LIMIT $offset, $pageSize");
		echo json_encode($rows);
		exit;
	}
	
	public function citydetailsAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$result = $db->fetchRow(
			"SELECT *,`default` cityDefault FROM uuch_city WHERE id = :id",
			array('id' => $this->_getParam('id'))
		);
		echo json_encode($result);
		exit;
	}
	
	public function cityeditAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
        $set = array (
			'name' => $this->_getParam('cityName'),
			'parentId' => (int)$this->_getParam('cityParentId'),
			'type' => $this->_getParam('cityType'),
			'ename' => $this->_getParam('cityEname'),
			'serviceFee' => $this->_getParam('cityServiceFee'),
			'sortid' => $this->_getParam('citySortid'),
			'percent' => $this->_getParam('cityPercent'),
			'state' => $this->_getParam('cityState'),
			'default' => $this->_getParam('cityDefault'),
		);
		$where = $db->quoteInto('id = ?', $this->_getParam('id'));
		$rows_affected = $db->update('uuch_city', $set, $where);
		echo 'editok';
		exit;
	}
	
	public function cityaddAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$row = array (
			'name' => $this->_getParam('cityName'),
			'parentId' => (int)$this->_getParam('cityParentId'),
			'type' => $this->_getParam('cityType'),
			'ename' => $this->_getParam('cityEname'),
			'serviceFee' => $this->_getParam('cityServiceFee'),
			'sortid' => $this->_getParam('citySortid'),
			'percent' => $this->_getParam('cityPercent'),
			'state' => $this->_getParam('cityState'),
			'default' => $this->_getParam('cityDefault'),
		);
		$rows_affected = $db->insert('uuch_city', $row);
		echo 'addok';
		exit;
	}
	
	public function citydelAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->delete('uuch_city', $where);
		echo json_encode(true);
		exit;
	}
	
	public function cityenableAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->update('uuch_city', array('state' => 1), $where);
		echo json_encode(true);
		exit;
	}
	
	public function citydisableAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->update('uuch_city', array('state' => 0), $where);
		echo json_encode(true);
		exit;
	}
	
	public function citydefaultAction() {
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');  
		$db = $bootstrap->getResource('db');
		$where = "id in ( '" . str_ireplace(',', "','", $this->_getParam('idList')) . "' )";
		$rows_affected = $db->update('uuch_city', array('default' => 1), $where);
		echo json_encode(true);
		exit;
	}
    
}